package io.navigation.modules.navigation.controller;

import io.navigation.common.utils.R;
import io.navigation.modules.navigation.entity.NavigationClassificationEntity;
import io.navigation.modules.navigation.entity.NavigationEntity;
import io.navigation.modules.navigation.service.NavigationClassificationService;
import io.navigation.modules.navigation.service.NavigationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("api/navigation/manualInput")
@Api(tags = "手动录入测试数据控制类")
public class ManualInputController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private NavigationClassificationService navigationClassificationService;

    /**
     * 分类信息导入测试方法
     */
    @GetMapping("/test")
    @ApiOperation("分类信息导入测试方法")
    public R test(){
        String[] html = {""};

        NavigationClassificationEntity nc = new NavigationClassificationEntity();
        nc.setCreateId("1");
        nc.setCreateTime(new Date());
        nc.setIsReveal(1);
        nc.setSort(0);
        nc.setIsPrivate(0);
        nc.setParentId("0");
        nc.setName("影视频道");
        navigationClassificationService.insert(nc);

        for (int m = 0; m < html.length; m++) {
            Document doc = Jsoup.parseBodyFragment(html[m]);
            Element body = doc.body();
            System.err.println(body.select("h4").text());
            Map<String, Object> map = new HashMap<>();
            map.put("name", body.select("h4").text());
            List<NavigationClassificationEntity> list = this.navigationClassificationService.selectByMap(map);
            NavigationClassificationEntity navigationClassification = new NavigationClassificationEntity();
            if (null != list && 0 < list.size()) {
                navigationClassification = list.get(0);
                navigationClassification.setParentId(nc.getId());
                navigationClassificationService.updateById(navigationClassification);
            } else {
                navigationClassification.setCreateId("1");
                navigationClassification.setCreateTime(new Date());
                navigationClassification.setIsReveal(1);
                navigationClassification.setSort(0);
                navigationClassification.setIsPrivate(0);
                navigationClassification.setParentId(nc.getId());
                navigationClassification.setName(body.select("h4").text());
                navigationClassificationService.insert(navigationClassification);
            }
            //获取最终网站信息
            Elements webs = body.getElementsByClass("xe-widget");
            for (int t = 0; t < webs.size(); t++) {
                Element web = webs.get(t);
                String name = web.select("strong").text();
                System.err.println("    "+name);
                String description = web.attr("data-original-title");
                System.err.println("    "+description);
                String url = web.attr("onclick");
                url = url.substring(url.indexOf("=")+1);
                url = url.substring(0,url.indexOf("'"));
                System.err.println("    "+url);
                System.err.println("    "+web.attr("onclick"));
                String ioce = "https://cache.wanyuanyin.cn/data/wukandy.cn"+web.select("img").attr("src").substring(1);
                System.err.println("    "+ioce);


                NavigationEntity navigationEntity = new NavigationEntity();
                navigationEntity.setClassificationId(navigationClassification.getId());
    //            navigationEntity.setClassificationId("0ce6d4edbca14e98b4bc4ac33c001341");
                navigationEntity.setCreateId("1");
                navigationEntity.setCreateTime(new Date());
                navigationEntity.setIsPrivate(0);
                navigationEntity.setIsReveal(1);
                navigationEntity.setName(filter(name));
                navigationEntity.setDescription(filter(description));
                navigationEntity.setSort(t);
                navigationEntity.setViewcount(0);
                navigationEntity.setWebsiteIcon(ioce);
                navigationEntity.setUrl(url);
                this.navigationService.insert(navigationEntity);

            }


//            Elements es = body.getElementsByClass("xe-widget");
//            int j = 0;
//            for (Element e : es) {
////                Elements ess = body.getElementsByClass("xe-comment");
//                System.err.println(e.attr("data-original-title"));
//                Elements strong = e.getElementsByTag("strong");
//                System.err.println(strong.text());
//                Elements p = e.getElementsByTag("p");
//                System.err.println(p.text());
//                Elements els = e.getElementsByTag("img");
//                System.err.println(els.get(0).attr("src"));
//
//            }
        }

        return R.ok();
    }
    /**
     * 过滤表情符号
     * @create by ldw on 2016-10-25
     * @param str
     * @return str(去掉表情符号的字符串)
     * @version 1.0
     * */
    public String filter(String str) {
        if (str.trim().isEmpty()) {
            return str;
        }
        String pattern = "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
        String reStr = "";
        Pattern emoji = Pattern.compile(pattern);
        Matcher emojiMatcher = emoji.matcher(str);
        str = emojiMatcher.replaceAll(reStr);
        return str;
    }
}
